package com.example.support.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.example.support.wechat.boot.AliPayProperties;
import com.example.foo.alipay.domain.params.AlipayBean;
import com.example.foo.order.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付宝支付接口
 */
@Component
public class Alipay {

    @Autowired
    private AliPayProperties aliPayProperties;

    /**
     * 电脑网站支付alipay.trade.page.pay接口
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    public String pay(AlipayBean alipayBean,String appId) throws AlipayApiException {

        AliPayProperties.Alipay alipay = this.aliPayProperties.getMappings().get(appId);

        // 1、获得初始化的AlipayClient
        String notifyUrl = alipay.getTrade().getNotifyUrl();
        AlipayClient alipayClient = initClient(alipay,appId);

        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
        return result;
    }

    /**
     * alipay.trade.app.pay：原生手机APP支付测试（外部商户APP唤起快捷SDK创建订单并支付）
     * @param
     * @return
     */
    public String aliAppPay(AlipayTradeAppPayModel model,String appId){
        try {

            AliPayProperties.Alipay alipay = this.aliPayProperties.getMappings().get(appId);

            // 1、获得初始化的AlipayClient

            String notifyUrl = alipay.getTrade().getNotifyUrl();
            AlipayClient alipayClient = initClient(alipay,appId);

            //（2）设置请求参数
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
            alipayRequest.setNotifyUrl(notifyUrl);
            alipayRequest.setBizModel(model);
            //（3）请求
            //这里和普通的接口调用不同，使用的是sdkExecute
            System.out.println(JSON.toJSONString(alipayRequest));
            String form = alipayClient.sdkExecute(alipayRequest).getBody();
            System.out.println("*********************\n返回结果为："+form);
            return form;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    public AlipayClient initClient(AliPayProperties.Alipay alipay,String appId){

        // 1、获得初始化的AlipayClient
        String serverUrl = alipay.getPay().getGatewayUrl();
        String privateKey = alipay.getPay().getPrivateKey();
        String format = "json";
        String charset = alipay.getOther().getCharset();
        String alipayPublicKey = alipay.getPay().getPublicKey();
        String signType = alipay.getOther().getSignType();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        return alipayClient;
    }

}
